package io.github.wanggit.antcms.server.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.wanggit.antcms.server.domain.AntArticle;
import io.github.wanggit.antcms.server.dto.ArticleListDTO;
import io.github.wanggit.antcms.server.service.impl.utils.DateUtil;
import io.github.wanggit.antcms.server.web.vm.ListArticlesVM;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface AntArticleMapper extends BaseMapper<AntArticle> {

    @Select(
            "<script>"
                    + " SELECT DISTINCT a.id, a.title, a.author_name, a.state, a.create_time, a.update_time, a.content_id "
                    + " FROM antcms.ant_article a"
                    + " LEFT JOIN antcms.ant_article_catalog b ON a.id = b.article_id"
                    + " LEFT JOIN antcms.ant_article_tag c ON a.id = c.article_id "
                    + " <where>"
                    + " <if test='catalogId != null'>"
                    + " AND b.catalog_id = #{catalogId}"
                    + " </if>"
                    + " <if test='tagId != null'>"
                    + " AND c.tag_id = #{tagId}"
                    + " </if>"
                    + " <if test='title != null'>"
                    + " AND a.title LIKE CONCAT('%', #{title}, '%')"
                    + " </if>"
                    + " </where>"
                    + " ORDER BY a.update_time DESC "
                    + " </script>")
    Page<AntArticle> queryArticles(
            Page<AntArticle> page,
            @Param("catalogId") Integer catalogId,
            @Param("tagId") Integer tagId,
            @Param("title") String title);

    @Select("SELECT DISTINCT DATE_FORMAT(a.update_time, '%Y-%m') FROM antcms.ant_article a")
    List<String> queryAllLastUpdateTime();

    @Select(
            "<script>"
                    + " SELECT a.id, GROUP_CONCAT(DISTINCT c.id) AS catalogIds, GROUP_CONCAT(DISTINCT c.name) AS catalogNames"
                    + " , GROUP_CONCAT(DISTINCT e.id) AS tagIds, GROUP_CONCAT(DISTINCT e.name) AS tagNames "
                    + " , a.title, a.state, a.create_time AS createTime, a.update_time AS updateTime, a.publish_time AS publishTime, a.url"
                    + " FROM antcms.ant_article a "
                    + " LEFT JOIN antcms.ant_article_catalog b ON a.id = b.article_id "
                    + " LEFT JOIN antcms.ant_catalog c ON b.catalog_id = c.id "
                    + " LEFT JOIN antcms.ant_article_tag d ON a.id = d.article_id"
                    + " LEFT JOIN antcms.ant_tag e ON d.tag_id = e.id "
                    + " <where>"
                    + " <if test='vm.titlePattern != null'> and a.title like concat('%', #{vm.titlePattern}, '%') </if>"
                    + " <if test='vm.state != null and vm.state != -1'> and a.state = #{vm.state}  </if>"
                    + " <if test='range.start != null and range.end != null'> <![CDATA[ AND a.update_time >= #{range.start} AND a.update_time < #{range.end} ]]> </if>"
                    + " <if test='vm.catalogId != null and vm.catalogId != -1'> AND c.id = #{vm.catalogId} </if>"
                    + " </where>"
                    + " GROUP BY a.id ORDER BY a.update_time DESC "
                    + "</script>")
    Page<ArticleListDTO> listArticles(
            @Param("vm") ListArticlesVM listArticlesVM,
            @Param("range") DateUtil.MonthRange monthRange,
            Page<ArticleListDTO> pageParams);
}
